using System;
using System.Collections;
using System.Globalization;
using Sedna.Core.Controller;
using Sedna.Core.Controls.Actions.Support;
using Sedna.Core.Controls.Support;
using Nullables;

namespace Sedna.Core.Controls
{
    /// <summary>
    /// Summary description for StockPopupButton.
    /// </summary>
    public class StockPopupButton : BasePopupButton
    {

        public object Value
        {
            get
            {
                string value = null;
                if (values.Count != 0)
                {
                    value = (string)values[0];
                }
                return Configuration.DataParser.Translate(this, value);
            }
            set
            {
                string newValue = (string)Configuration.DataTranslator.Translate(this, value);
                if (values.Count == 0)
                {
                    values.Add(newValue);
                }
                else
                {
                    values[0] = newValue;
                }
            }
        }


        public StockPopupButton(IConfiguration configuration)
            : base(configuration)
        {
            Text = "...";
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (Configuration.VisibleBindPath != null)
                this.Visible = true; // To reach the OnPreRender method and analyze visibility in the Bind method
        }

        protected override void Unbind()
        {
            binder.Unbind(new string[] { "Value", "Enabled", "Visible" });
        }

        protected override void Bind()
        {
            this.Visible = Configuration.Visible;

            binder.Bind(new string[] { "Value", "Enabled", "Visible" });

            this.Enabled = this.Enabled && Configuration.IsEnabledForPrincipal();
            this.Visible = this.Visible && Configuration.IsVisibleForPrincipal();
        }
    }
}
